<?php

# LemurCMS by Alec Gorge
#
#     This file contains the PHP functions used throught the CMS.
#
# updated: Jan. 12, 2009

class init extends themeManager {
	public $pageContent;
	public function __construct ($run = true) {
		if($run == true) {
			$this->register_site_info();
		}
		$this->db_connect();
	}
	public function loadPlugins () {
		$pluginDir = glob(BASE_URL.'plugins/*', GLOB_ONLYDIR);
		foreach ($pluginDir as $key => $value) {
			$indvPluginDir = end(explode('/', $value));
			$pluginFile = $value.'/plugin.php';
			if(file_exists($pluginFile)) {
				$r[] = $pluginFile;
			}
			else {
				$r = $admin->message('error', 'Plugin.php file not found for the plugin '.$indvPluginDir, 'Please contact the plugin developer to fix this.');
			}
		}
		return $r;
	}
	public function set_validation($user, $pass) {
		session_start();
		$_SESSION['username'] = sanitize_sql_string($user);
		$_SESSION['password'] = sha1(sanitize_sql_string($pass));
		echo $_SESSION['username'];
		if($this->validate() == false) {
			$_SESSION = array();
			session_destroy();
			$this->pageContent($this->adminLoginForm, 0);	
			$this->pageContent($this->message('error', 'Login Failed!', 'Login Failed. Try again.', -1));	
		}	
	}
	public function pageContent ($content, $append=0) {
		//$content = htmlspecialchars_decode(htmlentities($content, ENT_QUOTES, false)); 
		if($append == 1) {
			$this->page_info['content']['page_content'] .= $content;
		}
		if($append == -1) {
			$this->page_info['content']['page_content'] = $content.$this->page_info['content']['page_content'];
		}
		if($append == 0) {
			$this->page_info['content']['page_content'] = $content;
		}
	}
	public function pageTitle ($content, $append=0) {
		//$content = htmlspecialchars_decode(htmlentities($content, ENT_QUOTES, false)); 
		if($append == 1) {
			$this->page_info['content']['page_title'] .= $content;
		}
		if($append == -1) {
			$this->page_info['content']['page_title'] = $content.$this->page_info['content']['page_title'];
		}
		if($append == 0) {
			$this->page_info['content']['page_title'] = $content;
		}
	}
	public function display404Error () {
		$error404 = $this->getSetting('404 Error Message');
		$this->pageContent(trim(stripslashes(stripslashes($error404))));
		$this->pageTitle('Error 404: Page Not Found');
	}
	//$this->page_info['settings']['Home Page Id']['value']
	public function displayPage ($page_id = HOME_PAGE_ID, $set = 0) {
		$sql = "SELECT * FROM ".DBTABLEPREFIX."content_pages WHERE id ='$page_id'";
		$content = $this->db_query($sql) or die($this->db->error());
		if($this->db->numRows($content) == 0 || empty($page_id)) {
			$this->display404Error();
			return;
		}
		//var_dump($content); 
		//var_dump($this->db->fetchAssoc(), $this->db->numRows());
		while($row = $this->db->fetchArray($content)) {
			$this->pageTitle(trim(stripslashes(stripslashes(strip_tags(($row['title']))))));
			$this->pageContent(trim(stripslashes(stripslashes(stripslashes(stripslashes($row['content']))))));
		}
		if($set == 0) {
			return $this->page_info['content']['page_content'];
		}
	}
}

?>